package com.ibm.law.ui.system.setup;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;

import com.ibm.law.domain.FileTypeList;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.PojoObservables;
import com.ibm.law.domain.FileType;
import com.ibm.law.service.system.FileTypeService;

import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class FileTypeSetup extends Composite {
	private DataBindingContext m_bindingContext;
	private Table table;
	private FileTypeList fileTypeList=new FileTypeList();
	public FileTypeList getFileTypeList() {
		return fileTypeList;
	}

	public void setFileTypeList(FileTypeList fileTypeList) {
		this.fileTypeList = fileTypeList;
	}
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn;
	private TableViewerColumn tableViewerColumn_1;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public FileTypeSetup(Composite parent, int style) {

		super(parent, style);
		FileTypeService fileTypeService=new FileTypeService();
		fileTypeList.setList(fileTypeService.getAll());
		setLayout(new GridLayout(1, false));
		
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				tableViewer) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};

		TableViewerEditor.create(tableViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);
		
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(264);
		tblclmnNewColumn.setText("文件类型");
		
		tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("文件类型识别");
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new RowLayout(SWT.HORIZONTAL));
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileType fileType=new FileType();
				fileTypeList.addFileType(fileType);
				tableViewerColumn.getViewer().editElement(fileType, 0);
			}
		});
		btnNewButton.setText("增加");
		
		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setText("删除");
		
		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for(FileType f:fileTypeList.getList()){
					System.out.println(f);
				}
			}
		});
		btnNewButton_2.setText("保存");
		
		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.setText("退出");
		m_bindingContext = initDataBindings();

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = PojoObservables.observeMaps(listContentProvider.getKnownElements(), FileType.class, new String[]{"name", "code"});
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableList listFileTypeListObserveList = BeanProperties.list("list").observe(fileTypeList);
		tableViewer.setInput(listFileTypeListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		CellEditor cellEditor_1 = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty_1 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_1 = BeanProperties.value("code");
		tableViewerColumn_1.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
		//
		return bindingContext;
	}
}
